<?php
 //============================================================================
 // ____________________________PLIGG TEAM ADDON_______________________________
 //
// This CLASS was created by AnAlienHolakres3 ||| XXXXX  XX     XX XXXX   XXXX
// (c) 2007-2008                Jakub Holasek ||| XXXXX  XX     XX X      X
// @last_update                    27/02/2008 ||| XX     XX     XX X  XX  X  XX
// @version                              0.82  ||| XX     XXXXXX XX XXXXX  XXXXX
//
// This file can be used by Pligg community.
//=============================================================================

// Class TaskLinkship contains general information about task links. 
// Each of tasks can have one or more related links, this links can
// be added each of team members
// 
// Adding link to task is done by ajax system and related file save_tasklink.php
// and some functions in team.js

 

class TaskLinkhip {
 
    
    private $task_id=0; 
    private $team_id=0;
    private $user_id=0;
    private $link_id=0;
    
    private $tasklinks_data;  
    private $user_tasklinks_data;
    
    //============================================================================
    //  Function read_tasklinks() gets tasklinks data (array) from database   
    //============================================================================      
    
    function read_tasklinks() {
        global $db;
        
       $team_id = $this->team_id;     

      
      //get tasklinks of all tasks in team 

      $tasklinks = $db->get_results("SELECT ".table_tasklinkship.".*,user_login,link_url,link_title FROM " . table_tasklinkship.
       " JOIN " .table_users. " USING (user_id) JOIN " . table_links . " USING (link_id) WHERE  team_id=$team_id ");                                                
                                      
          if ($tasklinks) {   
              $this->tasklinks_data = $tasklinks;
               return true;
               $this->read = true;    
           }
              
              $this->read = false;
              return false;
    } 
    
    //============================================================================
    //  Function read_user_tasklinks() gets tasklinks data for specific user
    //  It means it get all team tasks which are in team and have task link
    //  If you want to use only for current user uncomment and see also 
    //  read_user_tasks() in task.php
    //============================================================================      
    
    function read_user_tasklinks() {
        global $db,$current_user;
        
       $team_id = $this->team_id;     

      
      //get tasklinks of all tasks in team 

   //   $user_tasklinks = $db->get_results("SELECT ".table_tasklinkship.".* FROM " . table_tasklinkship. " WHERE user_id=$current_user->user_id");                             
      $user_tasklinks = $db->get_results("SELECT ".table_tasklinkship.".* FROM " . table_tasklinkship. " 
      JOIN ". table_teams . " USING(team_id) WHERE team_id IN (SELECT team_id FROM ".table_teams . " WHERE team_creator_id=$current_user->user_id)
       OR team_id IN (SELECT team_id FROM ". table_membership . " WHERE user_id=$current_user->user_id)");                                                
                                      
          if ($user_tasklinks) {   
              $this->user_tasklinks_data = $user_tasklinks;
               return true;
               $this->read = true;    
           }
              
              $this->read = false;
              return false;
    } 
    
    
    //============================================================================
    //  Function fill_print() assign team task links smartys
    //============================================================================
      
      function fill_print()
      {
          global $main_smarty;                                   
              
              $tasklinks_data = $this->tasklinks_data;
              $user_data = $this->user_tasklinks_data;                           
                        
          if ($tasklinks_data ) {                   
               
               foreach ($tasklinks_data as $tasklink) {                      
                                                         
                  $tasklink_task_id[] = $tasklink->task_id;                 
                  $tasklink_link_id[] = $tasklink->link_id;
                  $tasklink_referer_name[] = $tasklink->user_login;
                  $tasklink_url[] = $tasklink->link_url;
                  $tasklink_story_url[] = getmyurl("story",$tasklink->link_id);
                  $tasklink_title[] = $tasklink->link_title; 
                 }
                                                           
                $main_smarty->assign('tasklink_task_id', $tasklink_task_id);               
                $main_smarty->assign('tasklink_link_id', $tasklink_link_id);                
                $main_smarty->assign('tasklink_referer_name', $tasklink_referer_name); 
                $main_smarty->assign('tasklink_url', $tasklink_url); 
                $main_smarty->assign('tasklink_story_url', $tasklink_story_url); 
                $main_smarty->assign('tasklink_title', $tasklink_title);                 
             } 
             
             if ($user_data ) {                   
               
               foreach ($user_data as $tasklink) {                      
                                                         
                  $tasklink_task_id[] = $tasklink->task_id;                  
                  $tasklink_link_id[] = $tasklink->link_id;
                  $tasklink_team_id[] = $tasklink->team_id;
                  
                 }
                                                           
                $main_smarty->assign('tasklink_task_id', $tasklink_task_id);
                $main_smarty->assign('tasklink_team_id', $tasklink_team_id);
                $main_smarty->assign('tasklink_link_id', $tasklink_link_id);                
                       
             }                                   
      } 
    
    //============================================================================
    //  Function store() save task link into database,, no updates availible
    //============================================================================
    
    function store() {
        global $db,$current_user;
    
        $user_id = $current_user->user_id;
        $task_id = $this->task_id;
        $team_id = $this->team_id;
        $link_id = $this->link_id;                
   
             $sql = "INSERT INTO " . table_tasklinkship. "(link_id, task_id, team_id,  user_id) VALUES ($link_id, $task_id, $team_id, $user_id)"; 
             
             if($db->query($sql)){   
              return true;     
              
              }
              return false;       
      
       }                                
    

      
      //==============================================================================
      // Function save_tasklink() saves task link values 
      //==============================================================================  
      
      function save_tasklink()
      {      global $current_user;
               
         
          //store to database
          if (!$this->store()) { 
              //cannot save to dabase
              return false;
          }
          return true;
      }
           
 //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 //  CHECKING FUNCTIONS
 //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     
      
    //============================================================================
    //  Function tasklink_already_added() finds out if task link is already saved
    //  @return true if it is saved otherwise false
    //============================================================================      
    
    function tasklink_already_added() {
        global $db;         
        $task_id = $this->task_id;
        $team_id = $this->team_id;
        $link_id = $this->link_id;
       
        $exist=$db->get_var("SELECT count(*) FROM ". table_tasklinkship. " WHERE   
                            team_id=$team_id AND task_id=$task_id AND link_id=$link_id");               
                                 
        if ($exist != 0) { 
         return true; 
        }
        return false;
    }
    
      

 //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 //  SETTING FUNCTIONS
 //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
   
    //============================================================================
    //  Function set_team_id() sets taskrating team_id
    //  @param team_id
    //============================================================================      
    function set_team_id($team_id) {
     $this->team_id = $team_id;
    } 
   
   //============================================================================
    //  Function set_task_id() sets taskrating task_id
    //  @param task_id
    //============================================================================      
    function set_task_id($task_id) {
     $this->task_id = $task_id;
    } 
     //============================================================================
    //  Function set_user_id() sets taskrating user_id
    //  @param user_id
    //============================================================================      
    function set_user_id($user_id) {
     $this->user_id = $user_id;
    } 
     //============================================================================
    //  Function set_link_id() sets link id
    //  @param link_id
    //============================================================================      
    function set_link_id($link_id) {
     $this->link_id = $link_id ;
    }
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    //  GETTING FUNCTIONS
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

                        
    
}
?>

